//package com.library.uiframework.paging;
//
//import java.util.List;
//
//import android.os.Bundle;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import com.taobao.uikit.extend.feature.features.ImageLoadFeature;
//import com.taobao.uikit.feature.view.TImageView;
//import com.youku.uiframework.R;
//import com.youku.uiframework.StateViewFragment;
//import com.youku.uiframework.nuwa.NuwaRecyclerViewAdapter;
//import com.youku.uiframework.widget.PlanetRefreshHeader;
//import com.youku.uiframework.widget.StateView;
//import com.youku.widget.XRecyclerView;
//
//import static java.security.AccessController.getContext;
//
///**
// * @author hongkai.qian
// * @version 1.0.0
// * @since 17/3/17
// */
//abstract public class PagingRecyclerViewFragment<VIEW extends PagingDataLoadView> extends StateViewFragment
//        implements PagingDataLoadView {
//
//    private static final String TAG = "RecyclerViewList";
//
////    private PtrFrameLayout mPtrFrameLayout;
//
//    private XRecyclerView mRecyclerView;
//
//    private PlanetRefreshHeader mPlanetRefreshHeader;
//
//    private IPagingListAdapter mAdapter;
//
//    private PagingDataLoadPresenter<VIEW> mPresenter;
//
////    private LoadingFooterView mFooterView;
//
//    protected int listViewLayoutId() {
//        return needPtrAndLoadNextFeature() ? R.layout.uiframework_recyclerview_with_ptr : R.layout.uiframework_recyclerview_without_ptr;
//    }
//
//    /**
//     * @return 实现IPagingListAdapter接口, 同时需要派生自RecyclerView.Adapter
//     * <p>
//     * 可选择 NuwaRecyclerViewAdapter 或者 PagingRecyclerViewAdapter 的派生类
//     */
//    protected abstract IPagingListAdapter onCreateAdapter();
//
//    /**
//     * 给业务一个加工contentView的机会, 比如再包装一层
//     *
//     * @param contentView contentView
//     * @return contentView
//     */
//    protected View onContentViewInflated(View contentView) {
//        return contentView;
//    }
//
//    protected abstract PagingDataLoadPresenter<VIEW> onCreatePresenter();
//
//    protected boolean needPtrAndLoadNextFeature() {
//        return true;
//    }
//
//    public PlanetRefreshHeader getRefreshHeaderView() {
//        return mPlanetRefreshHeader;
//    }
//
//    @Override
//    protected View onCreateContentView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
//        View mainView = layoutInflater.inflate(listViewLayoutId(), viewGroup, false);
////        mPtrFrameLayout = (PtrFrameLayout) mainView.findViewById(R.id.ptr_frame_layout);
//        mRecyclerView = (XRecyclerView) mainView.findViewById(R.id.recycler_view);
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(layoutInflater.getContext(), LinearLayoutManager.VERTICAL, false));
//
//        mRecyclerView.setHasFixedSize(true);
//        mRecyclerView.setNoMoreHintStay(false);
//
//        mRecyclerView.setPullRefreshEnabled(true);
//        mPlanetRefreshHeader = new PlanetRefreshHeader(getContext());
//        mRecyclerView.setRefreshHeader(mPlanetRefreshHeader);
//        if (needPtrAndLoadNextFeature()) {
//            mRecyclerView.setLoadingMoreEnabled(true);
//        } else {
//            mRecyclerView.setPullRefreshEnabled(false);
//        }
//
//        mAdapter = onCreateAdapter();
//        RecyclerView.Adapter adapter = (RecyclerView.Adapter) mAdapter;
//        mRecyclerView.setAdapter(adapter);
//        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
//            @Override
//            public void onRefresh() {
//                mPresenter.reload(true);
//            }
//
//            @Override
//            public void onLoadMore() {
//                if (mPresenter.canAutoLoadNextPage()) {
//                    mPresenter.loadNextPage();
//                } else {
//                    refreshComplete();
//                }
//            }
//        });
//
////        if (enableSmoothImageLoad()) {
////            mRecyclerView.addOnScrollListener(mOnScrollListener);
////        }
//
////        if (mPtrFrameLayout != null) {
////            mPtrFrameLayout.disableWhenHorizontalMove(true);
////            mFooterView = new LoadingFooterView(layoutInflater.getContext());
////            mFooterView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT
////                    , RecyclerView.LayoutParams.WRAP_CONTENT));
////            mRecyclerView.setAdapter(new WrapperRecyclerFooterAdapter(adapter, mFooterView));
////
////            mFooterView.setOnClickListener(new View.OnClickListener() {
////                @Override
////                public void onClick(View v) {
////                    mPresenter.loadNextPage();
////                }
////            });
////
////            mRecyclerView.addOnScrollListener(new OnLoadMoreListener() {
////                @Override
////                public void onLoadMore(RecyclerView recyclerView) {
////                    if (mPresenter.canAutoLoadNextPage()) {
////                        mPresenter.loadNextPage();
////                    } else {
////                        refreshComplete();
////                    }
////
////                }
////            });
////
////        } else {
////            mRecyclerView.setAdapter(adapter);
////        }
//
////        if (mPtrFrameLayout != null) {
////            mPtrFrameLayout.setPtrHandler(new PtrHandler() {
////
////                @Override
////                public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
//////                    if (!AppRuntime.Network.isNetWorkAvailable()) {
//////                        ToastUtils.showToast(R.string.network_unavailable);
//////                        ptrFrameLayout.refreshComplete();
//////                        return;
//////                    }
////                    mPresenter.reload(true);
////                }
////
////                @Override
////                public boolean checkCanDoRefresh(PtrFrameLayout ptrFrameLayout, View content, View header) {
////                    return PtrDefaultHandler.checkContentCanBePulledDown(ptrFrameLayout, content, header);
////                }
////            });
////        }
//
//        return onContentViewInflated(mainView);
//    }
//
//    protected boolean enableSmoothImageLoad() {
//        return true;
//    }
//
//    private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
//        @Override
//        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//            super.onScrolled(recyclerView, dx, dy);
//        }
//
//        @Override
//        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//            super.onScrollStateChanged(recyclerView, newState);
//            if (getActivity().isFinishing()) {
//                return;
//            }
//            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                resume(recyclerView);
//            } else {
//                pause(recyclerView);
//            }
//        }
//
//        private ImageLoadFeature getImageLoadFeature(TImageView view) {
//            return (ImageLoadFeature)view.findFeature(ImageLoadFeature.class);
//        }
//
//        private void resume(View v) {
//            if(v instanceof ViewGroup) {
//                ViewGroup imageLoad = (ViewGroup)v;
//                int count = imageLoad.getChildCount();
//
//                for(int i = 0; i < count; ++i) {
//                    this.resume(imageLoad.getChildAt(i));
//                }
//            } else if(v instanceof TImageView) {
//                ImageLoadFeature var5 = this.getImageLoadFeature((TImageView)v);
//                if(var5 != null) {
//                    var5.resume();
//                }
//            }
//
//        }
//
//        private void pause(View v) {
//            if(v instanceof ViewGroup) {
//                ViewGroup imageLoad = (ViewGroup)v;
//                int count = imageLoad.getChildCount();
//
//                for(int i = 0; i < count; ++i) {
//                    this.pause(imageLoad.getChildAt(i));
//                }
//            } else if(v instanceof TImageView) {
//                ImageLoadFeature var5 = this.getImageLoadFeature((TImageView)v);
//                if(var5 != null) {
//                    var5.pause();
//                }
//            }
//
//        }
//    };
//
//    @Override
//    public void onViewCreated(View view, Bundle savedInstanceState) {
//        mPresenter = onCreatePresenter();
//        super.onViewCreated(view, savedInstanceState);
//    }
//
//    @Override
//    protected void registerPageLifecycles() {
//        super.registerPageLifecycles();
//        if (mPresenter != null) {
//            registerPageLifecycle(mPresenter);
//        }
//        if (mAdapter instanceof NuwaRecyclerViewAdapter) {
//            NuwaRecyclerViewAdapter adapter = (NuwaRecyclerViewAdapter) mAdapter;
//            registerPageLifecycle(adapter);
//        }
//    }
//
//    @Override
//    public void onLoadFinished() {
//        super.onLoadFinished();
//        if (enterAutoLoading()) {
////            List<> cacheData = onReloadCacheData();
////            if (cacheData != null && !cacheData.isEmpty()) {
////                mAdapter.flushData(cacheData);
////                setState(StateView.State.SUCCESS);
////            }
//            mPresenter.reload(false);
//        }
//    }
//
//    protected boolean enterAutoLoading() {
//        return true;
//    }
//
//    @Override
//    protected final StateView.State originalState() {
//        return enterAutoLoading() ? StateView.State.LOADING : StateView.State.SUCCESS;
//    }
//
//    @Override
//    protected void onRetryRequested() {
//        mPresenter.reload(false);
//    }
//
//    /**
//     * 吸收listview换成recyclerView的经验教训, 此方法如无必要尽量不要调用
//     * 主要便于将来更换成其他更牛逼的XxxxView使业务无感
//     * 如果觉得想要进行的操作比较通用, 可提出来放到基类支持
//     *
//     * @return 当前页面的RecyclerView
//     */
//    protected RecyclerView getRecyclerView() {
//        return mRecyclerView;
//    }
//
//    /**
//     * 增加headerView，在onContentViewInflated或者onViewCreated里加
//     *
//     * @param view view
//     */
//    public void addHeaderView(View view) {
//        if (view.getLayoutParams() == null) {
//            view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        }
//        mRecyclerView.addHeaderView(view);
//    }
//
////    /**
////     * 增加footerView，在onContentViewInflated或者onViewCreated里加
////     *
////     * @param view view
////     */
////    public void addFooterView(View view) {
////        mRecyclerView.addFootView(view);
////    }
//
//    protected void refreshComplete() {
////        if (mPtrFrameLayout != null) {
////            mPtrFrameLayout.refreshComplete();
////        }
//        mRecyclerView.refreshComplete();
//    }
//
//    @Override
//    public void flushData(List dataList) {
//        mAdapter.flushData(dataList);
//    }
//
//    @Override
//    public void appendData(List dataList) {
//        mAdapter.appendData(dataList);
//    }
//
//    @Override
//    public void showNextPageLoading() {
////        mFooterView.setState(LoadingFooterView.State.Loading);
//    }
//
//    @Override
//    public void showNextPageFailure() {
////        mFooterView.setState(LoadingFooterView.State.NetWorkError);
//        mRecyclerView.setNoMore(false);
////        mRecyclerView.loadMoreComplete();
//    }
//
//    @Override
//    public void showNextPageSuccess() {
////        mFooterView.setState(LoadingFooterView.State.Normal);
//        mRecyclerView.setNoMore(false);
////        mRecyclerView.loadMoreComplete();
//    }
//
//    @Override
//    public void showAllPageLoaded() {
////        mFooterView.setState(LoadingFooterView.State.TheEnd);
//        mRecyclerView.setNoMore(true);
//    }
//
//    @Override
//    public void showLoading() {
//        setState(StateView.State.LOADING);
//    }
//
//    @Override
//    public void showFailure(String message) {
//        refreshComplete();
//        setState(StateView.State.FAILED);
//    }
//
//    @Override
//    public void showNoData() {
//        refreshComplete();
//        setState(StateView.State.NO_DATA);
//    }
//
//    @Override
//    public void showSuccess() {
//        refreshComplete();
//        setState(StateView.State.SUCCESS);
//    }
//
//    @Override
//    public boolean isDataEmpty() {
//        return mAdapter == null || mAdapter.getRealCount() <= 0;
//    }
//}
